package org.quadbac.converter;

import org.quadbac.measurements.*;

import android.app.Activity;
import android.os.Bundle;

import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class AddType extends Activity {
	private EditText editTypeName;
	private EditText editUnitName;
	private EditText editUnitSymbol;
	private MeasurementType mType;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addtype);
        
        editTypeName = (EditText) findViewById(R.id.editTypeName);
        editUnitName = (EditText) findViewById(R.id.editUnitName);
        editUnitSymbol = (EditText) findViewById(R.id.editUnitSymbol);
        
        editTypeName.setText("");
        editUnitName.setText("");
        editUnitSymbol.setText("");
    }
 
    public void createButtonHandler(View view) {
    	if (validateNewType()){
        	mType = new MeasurementType(editTypeName.getText().toString(), "");
        	Unit initUnit = new Unit(editUnitName.getText().toString(), editUnitSymbol.getText().toString(), new LinearTransform(0.0,1.0));
        	mType.add(initUnit);
        	getIntent().putExtra("newType", mType);
        	setResult(RESULT_OK, getIntent());
        	finish();
    	}
    }
    
    private boolean validateNewType() {
    	
    	if (editTypeName.getText().length() == 0){
			editTypeName.requestFocus();
			Toast t = Toast.makeText(this,"Please enter a type name",Toast.LENGTH_LONG);
	    	t.setGravity(Gravity.CENTER, 0, 0);
			t.show();
			return false;
		}
		if (editUnitName.getText().length() == 0){
			editUnitName.requestFocus();
			Toast t = Toast.makeText(this,"Please enter a unit name",Toast.LENGTH_LONG);
	    	t.setGravity(Gravity.CENTER, 0, 0);
			t.show();
			return false;
		}
		return true;
	}
}